package com.hfut.eeg.common.component;

import com.hfut.eeg.common.util.EEGUtil;
import com.hfut.eeg.common.util.ReadUtil;
import com.hfut.eeg.socket.AiServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;

/**
 * @author 梁琦
 * @program: eeg
 * @Package com.hfut.eeg.common.component
 * @Description: 读取本地文件信息，注意，只运行一次
 * @date 2019-12-21 16:17
 */
@Component
@Scope("singleton")
public class EEGLocalFileReader {
    private static final Logger LOGGER = LoggerFactory.getLogger(EEGLocalFileReader.class);
    private String patientId = "BH20191214004";

    //是否启动即运行
    private boolean isStartUpRun = false;

    private boolean isAiPrepared = false;

    @Autowired
    private AiServer aiServer;

    @Autowired
    private EEGcache eeGcache;

    @Autowired
    private  ReadUtil readUtil;

    public boolean isAiPrepared() {
        return isAiPrepared;
    }

    public void setAiPrepared(boolean aiPrepared) {
        isAiPrepared = aiPrepared;
    }

    /**
    * 读取本地文件，将本地文件数据进行处理，然后入库
    */
    public void readerRun() throws InterruptedException {
        int loopSum=0;
        if(isStartUpRun){
            aiServer.setEegId(patientId);
            eeGcache.setPatientId(patientId);
            while (true){
                Thread.sleep(1);
                if(isAiPrepared){
                    try {
                        loopSum++;
                        ArrayList<ArrayList>lists=readUtil.getData(loopSum);
                        if(lists==null || lists.size()==0){
                            break;
                        }
                        ArrayList<ArrayList> tmp = EEGUtil.deepCopy(lists);
                        tmp.add(new ArrayList());
                        tmp.add(new ArrayList());
                        aiServer.setCachePools(tmp);
                        eeGcache.addDataToPool(EEGUtil.deepCopy(lists));
                        Thread.sleep(900);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
